package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.ExamPaperMapper;
import com.study.happystudy.po.ExamPaper;
import com.study.happystudy.po.Paper;
import com.study.happystudy.po.Question;
import com.study.happystudy.service.ExamPaperService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamPaperServiceImpl {

    @Resource
    ExamPaperMapper examPaperMapper;


    public void insertExamPaper(@Param("startTime") String startTime,
                                @Param("endTime") String endTime,
                                @Param("examType") String examType,
                                @Param("grade") int grade,
                                @Param("name") String name,
                                @Param("subjectId") int subjectId,
                                @Param("totalScore") int totalScore,
                                @Param("userId") int userId
    ) {
        examPaperMapper.insertExamPaper(startTime, endTime, examType, grade, name, subjectId, totalScore, userId);
    }

    public void removeExamPaperById(int examPaperId) {
        examPaperMapper.deleteExamPaperById(examPaperId);
    }


    public ExamPaper getExamPaperById(int examPaperId) {
        ExamPaper ex =  examPaperMapper.getExamPaperById(examPaperId);
        ex.setQuestions(examPaperMapper.listQuestionsByExamPaperId(examPaperId));
        return ex;
    }


    public void updateExamPaper(int examPaperId, String name, String examType, int grade, int subjectId, int totalScore, String startTime, String endTime) {
        examPaperMapper.updateExamPaper(examPaperId, name, examType, grade, subjectId, totalScore, startTime, endTime);
    }


    public List<ExamPaper> findExamPapersByGradeAndSubject(int grade, int subjectId) {
        return examPaperMapper.findExamPapersByGradeAndSubject(grade, subjectId);
    }


    public void assignExamPaperToStudent(int studentId, int examPaperId) {
        examPaperMapper.assignExamPaperToStudent(studentId, examPaperId);
    }


    public List<Question> listQuestionsByExamPaperId(int examPaperId) {
        return examPaperMapper.listQuestionsByExamPaperId(examPaperId);
    }
    public void insertQuestion(int examPaperId, int questionId, int order) {
        examPaperMapper.insertQuestion(examPaperId, questionId,order);
    }

    public List<Paper> getExamPapersBySubjectId(Integer subjectId){
        try {
            List<ExamPaper> list = examPaperMapper.getExamPapersBySubjectId(subjectId);
            return list.stream().map(Paper::toPaper).toList();
        }catch (Exception e){
            return null;
        }
    }
}
